import Vue from 'vue'
import Vuex from 'vuex'
import {
  setItem,
  getItem,
  removeItem2
} from "common/localStorage"

import {
  MessageBox
} from 'mint-ui';

Vue.use(Vuex)

import ShopCart from "./shopCart";

let store = new Vuex.Store({
  state: {
    isLogin: true,
    userInfo: {
      name: "Wooong",
      email: "48218471@163.com",
      phone: "1857712948",
      gender: "male",
      balance: 23141.12,
      avatar: "1.jpg",
      shippingAddrs: [{
        id: 0,
        province: "广东省",
        city: "广州市",
        county: "",
        town: "",
        other: "东风东路745号",
        phone: "1824771214",
        consigneeName: "陈祥旭",
      }, {
        id: 1,
        province: "广东省",
        city: "广州市",
        county: "",
        town: "",
        other: "越秀区德政中路310号",
        phone: "5161713513",
        consigneeName: "陈锐句",
      }, {
        id: 2,
        province: "广东省",
        city: "广州市",
        county: "",
        town: "",
        other: "荔湾区福源一街17号",
        phone: "12551135166",
        consigneeName: "陈大镇",
      }]
    }
  },
  getters: {
    getFullAddr(state) {
      return (id) => state.userInfo.shippingAddrs[id].province + " " + state.userInfo.shippingAddrs[id].city + " " + state.userInfo.shippingAddrs[id].county + " " + state.userInfo.shippingAddrs[id].town;
    }
  },
  mutations: {
    loginOut(state) {
      MessageBox.confirm('确定退出此账号?')
        .then(action => {
          if (action == "confirm") {
            state.isLogin = false;
          }
        }).catch(() => {});

      removeItem2("userInfo");
    },
    loginIn(state) {
      state.isLogin = true;

      let userInfo = getItem("userInfo");
      if (userInfo != null) {
        state.userInfo = JSON.parse(userInfo);
      }
      let cart = getItem("cart");
      if (cart != null) {
        state.cart = JSON.parse(cart);
      }
    },
    updateAddr(state, preload) {
      // return new Promise((resolve, reject) => {
      MessageBox.confirm('确定保存修改?')
        .then(action => {
          if (action == "confirm") {
            // resolve(action)
            preload.callback();
            let addr = state.userInfo.shippingAddrs[Number(preload.id)];
            for (const prop in addr) {
              state.userInfo.shippingAddrs[Number(preload.id)][prop] = preload.addrInfo[prop];
            }

            console.log(state);
          }
        }).catch(err => {
          // reject(err)
          console.log(err);
        });
      // })
    },
    addAddr(state, payload) {
      state.userInfo.shippingAddrs.splice(0, 0, payload);
      MessageBox('提示', '添加成功').then(() => {
        payload.callback();
      });
    },

    saveInfo(state) {
      let jsonUserInfo = JSON.stringify(state.userInfo);
      setItem("userInfo", jsonUserInfo);
    },
    getInfo(state) {
      let userInfo = getItem("userInfo");
      if (userInfo != null) {
        state.userInfo = JSON.parse(userInfo);
      }
    }
  },
  actions: {},
  modules: {
    cart: ShopCart
  },
})

export default store;